package com.tas.editor.managers
{
	import com.tas.movies.Movie;
	import com.tas.movies.MovieEvent;
	import com.tas.movies.formats.SNES9x;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	
	[Bindable]
	public class MovieManager extends EventDispatcher
	{		
		/**
		 * @private 
		 * 
		 * Constructor
		 */		
		public function MovieManager()
		{
			super();
			
			if( instance != null ){
				throw new Error( "A singleton cannot be directly instantiated. Use getInstance()." );
			}												
		}
		
		// Singleton Initialization ////////////////////////
		
		private static var instance : MovieManager;
		
		public static function getInstance() : MovieManager
		{
			if( instance == null ){
				instance = new MovieManager();				
			}
			
			return instance;
		}				
		
		////////////////////////////////////////////////////
		
		/**
		 * A reference to the currently loaded movie object 
		 */		
		public var movie : Movie;
		
		/**
		 * Bound to the application statusBar 
		 */		
		public var statusMessage : String;					
					
		/**
		 * Utility function to simplify basic event dispatching
		 *  
		 * @param event the name of the event to fire		 
		 */		
		public function fireEvent( event:String ) : void
		{
			dispatchEvent( new Event(event) );
		}
		
		public function browse() : void
		{
			var filters : Array = [
				new FileFilter( "Snes9x", "*.smv" ) ];
				
			
			var file : File = new File();
			file.browseForOpen( "Load a TAS Movie File", filters );
			file.addEventListener( Event.SELECT, onFileSelected );
		}
		
		private function onFileSelected( event:Event ) : void
		{
			var file : File = event.currentTarget as File;
			
			movie = new SNES9x();
			movie.addEventListener( MovieEvent.PROPERTIES_LOADED,
				function( e:Event ) : void
				{
					fireEvent( "moviePropertiesLoaded" );
				}, false, 0, true );
				
			movie.filename = file.nativePath;
		}
		
		public function loadFile( file:String ) : void
		{
			movie.filename = file;
		}				
	}
}